$(function () {
    // 初始化内容
    // 初始化内容
    $("#myAlert").css('display', 'none');
    $("#monitorMenu").attr("class", "active");
    $("#breadcrumb").html("监控列表");
    pagehtml(0);
    $("#btnSearch").click(function () {
        pagehtml(0);
    });

    $("#editType").click(function () {
        $('#editTypeTimeModal').modal();
    });
    $("#editCostTimeBtn").click(function () {
        var id = $("#accountId").val();
        var costTime = $("#costTime").val();
        var intervalTime = $("#intervalTime").val();
        var timeoutTime = $("#timeoutTime").val();
        var account = $("#accountNo").val();
        var editAccountType = $("#editAccountType").val();
        var editType = $("#editType").val();
        var remark = $("#remark").val();
        var basePath = getContextPath();
        $.post(basePath + "/back/monitor/setCostTime",
            {
                id: id,
                costTime: costTime,
                intervalTime: intervalTime,
                timeoutTime: timeoutTime,
                accountType: editAccountType,
                type: editType,
                account:account,
                remark:remark
            }, function (data) {
                $("#myAlert").css('display', 'block');
                $("#alertMsg").html(data.message);
                setTimeout(function () {
                    window.location.reload()
                }, 2000);
            })
    });

    $("#addMonitor").click(function () {
        $("#accountId").val("");
        $("#accountNo").val("");
        $("#costTime").val("");
        $("#intervalTime").val("");
        $("#timeoutTime").val("");
        $("#editType").val("");
        $("#remark").val("");
        $("#editAccountType").val("");
        $("#myModalLabel").text("添加账号");
        $("#accountNo").removeAttr("readonly");
        $('#myModal').modal();
    });
});

function delMonitor(id) {
    Ewin.confirm({message: "确认要删除选择的数据吗？"}).on(function (e) {
        if (!e) {
            return;
        }
        var basePath = getContextPath();
        $.post(basePath + "/back/monitor/del/" + id, function (data) {
            $("#myAlert").css('display', 'block');
            $("#alertMsg").html(data.message);
            setTimeout(function () {
                window.location.reload()
            }, 2000);
        });
    });
}

function setTime(obj) {
    $("#accountId").val(obj.id);
    $("#accountNo").val(obj.account);
    $("#costTime").val(obj.costTime);
    $("#intervalTime").val(obj.intervalTime);
    $("#timeoutTime").val(obj.timeoutTime);
    $("#editType").val(obj.type);
    $("#editAccountType").val(obj.accountType);
    $("#remark").val(obj.remark);
    $("#myModalLabel").text("修改报警间隔时间");
    $('#myModal').modal();
}
function editWarning(id,isWarning){
    var basePath = getContextPath();
    $.post(basePath + "/back/monitor/editWarning",{
        id:id,
        isWarning:isWarning
    }, function (data) {
        $("#myAlert").css('display', 'block');
        $("#alertMsg").html(data.message);
        setTimeout(function () {
            window.location.reload()
        }, 2000);
    });
}
function pagehtml(pageNum) {
    if (pageNum == 0) {
        pageNum = 1;
    } else {
        pageNum = pageNum;
    }
    var bizId = $("#bizId").val();
    var account = $("#account").val();
    var accountType = $("#accountType").val();
    var type = $("#type").val();
    var basePath = getContextPath();
    $.ajax({
        url: basePath + "/back/monitor/list",
        type: "post",
        dataType: "json",
        data: {
            pageNum: pageNum,
            pageSize: 20,
            account: account,
            accountType: accountType,
            type: type,
            bizId: bizId
        },
        success: function (data) {
            var options = {
                bootstrapMajorVersion: 3,//bootstrap版本
                alignment: "center",//居中显示
                currentPage: data.pageNum,//当前页数
                totalPages: data.totalPages,//总页数 注意不是总条数
                pageUrl: function (type, page, current) {
                    if (page == current) {
                        return "javascript:void(0)";
                    } else {
                        return "javascript:pagehtml(" + page + ")";
                    }
                }
            };
            var html = "";
            $.each(data.list, function (i, obj) {
                html += "<tr >" +
                    "<td>" + obj.id + "</td>" +
                    "<td>" + obj.account + "</td>";
                if (obj.isWarning == '010') {
                    html += "<td><a href='#' class='btn btn-success' onclick='editWarning("+obj.id+",\"020\")'>是</a></td>";
                } else if (obj.isWarning == '020') {
                    html += "<td><a href='#' class='btn btn-warning' onclick='editWarning("+obj.id+",\"010\")'>否</a></td>";
                }
                if (obj.accountType == '010') {
                    html += "<td><a href='#' class='btn btn-success'>低频</a></td>";
                } else if (obj.accountType == '020') {
                    html += "<td><a href='#' class='btn btn-info'>活跃</a></td>";
                } else if (obj.accountType == '030') {
                    html += "<td><a href='#' class='btn btn-success'>高频</a></td>";
                } else if (obj.accountType == '040') {
                    html += "<td><a href='#' class='btn btn-info'>T-1</a></td>";
                } else if (obj.accountType == '050') {
                    html += "<td><a href='#' class='btn btn-success'>40/次</a></td>";
                } else if (obj.accountType == '060') {
                    html += "<td><a href='#' class='btn btn-success'>20/次</a></td>";
                }else {
                    html += "<td><a href='#' class='btn btn-info'>其他</a></td>";
                }
                html += "<td>" + obj.reqTime + "</td>" +
                    "<td>" + obj.retTime + "</td>" +
                    "<td>" + obj.costTime + "</td>" +
                    "<td>" + obj.timeoutTime + "</td>" +
                    "<td>" + obj.intervalTime + "</td>";
                if (obj.type == '010') {
                    html += "<td><a href='#' class='btn btn-success'>当日</a></td>";
                } else if (obj.type == '020') {
                    html += "<td><a href='#' class='btn btn-info'>历史</a></td>";
                }
                html += "<td>" + obj.bankName + "</td>" +
                    "<td>" + obj.reqCount + "</td>" +
                    "<td>" + obj.invalidCount + "</td>" +
                    "<td>" + obj.validCount + "</td>" +
                    "<td>" + obj.faileCount + "</td>";
                if (obj.retStatus == '010') {
                    html += "<td><a href='#' class='btn btn-info'>初始化</a></td>";
                } else if (obj.retStatus == '020') {
                    html += "<td><a href='#' class='btn btn-success'>有效</a></td>";
                } else if (obj.retStatus == '030') {
                    html += "<td><a href='#' class='btn btn-danger'>失败</a></td>";
                } else if (obj.retStatus == '040') {
                    html += "<td><a href='#' class='btn btn-warning'>无效</a></td>";
                }
                html += "<td>" + obj.remark + "</td>" +
                    "<td>" + obj.createTime + "</td>" +
                    "<td>" + obj.updateTime + "</td>" +
                    "<td>" +
                    "<a href='#' class='btn btn-success' onclick='setTime(" + JSON.stringify(obj) + ")'>修改</a> | " +
                    "<a href='#' class='btn btn-danger' onclick='delMonitor(" + obj.id + ")'>删除</a> " +
                    "</td>" +
                    "</tr>";

            });
            $("#content").empty();
            $("#accountCount").html(data.total);
            $("#content").append(html);
            $('#data-pagination').bootstrapPaginator(options);
        },
        error: function (error) {
            alert("error");
        }
    });
}
